# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _
import time

import logging

_logger = logging.getLogger(__name__)


def virtual_request_method(fapiao):
    print('virtual_request_method', fapiao)
    time.sleep(3)
    return [{'id': one.id} for one in fapiao]


def virtual_query_method(fapiao):
    print('virtual_query_method', fapiao)
    time.sleep(3)
    return [{
        'id': one.id,
        'state': 'done',

        'taxmachine_code': one.taxmachine_code,
        'payee_name': one.payee_name,
        'checker_name': one.checker_name,
        'drawer_name': one.drawer_name,

        'date_fapiao': one.fapiao_id.date_fapiao,
        'time_fapiao': one.fapiao_id.time_fapiao,

        'invoice_code': 'invoice_code_virtual',
        'invoice_number': 'invoice_number_virtual',
        'check_code': 'check_code_virtual',

        'amount_untaxed': one.fapiao_id.amount_untaxed,
        'amount_tax': one.fapiao_id.amount_tax,
        'amount_total': one.fapiao_id.amount_total,
        'line_ids': [{
            # TODO 区分 折扣行和 普通行
            'id': line.id,
            'tax_ratio': line.tax_ratio,
            'quantity': line.quantity,
            'amount_untaxed': line.amount_untaxed,
            'amount_tax': line.amount_tax,
            'amount_total': line.amount_total,
            'price_untax': line.price_untax,
            'price_unit': line.price_unit,
        } for line in one.fapiao_id.line_ids
        ]

    } for one in fapiao]


def virtual_invalid_method(fapiao):
    print('virtual_invalid_method', fapiao)
    time.sleep(3)
    return [{'id': one.id} for one in fapiao]


def virtual_print_method(fapiao):
    print('virtual_print_method', fapiao)
    time.sleep(3)
    return [{'id': one.id} for one in fapiao]


class FapiaoApiDraw(models.Model):
    _name = "fp.api.draw"
    _description = "Fapiao Api Draw"
    _order = 'sequence, name'

    name = fields.Char(
        string='Provider name', required=True,
        help="Name of Provider, Baiwang, Aisino, etc")

    code = fields.Char(
        string='Provider Code', required=True,
        help="Code of Provider, Baiwang, Aisino, etc, Unique")

    endpoint = fields.Char(
        string='URL', required=True, help="provider URL")

    sequence = fields.Integer(default=10)

    _sql_constraints = [
        ('code_key', 'UNIQUE (code)',
         'You can not set twe api providers for the same code !')
    ]

    def _request_methods(self):
        # to override.
        # update other  request_method
        return {'virtual': virtual_request_method}

    def _request_method_get(self):
        methods = self._request_methods()
        return methods.get(self.code)

    def _query_methods(self):
        # to override.
        # update other  _query_methods
        return {'virtual': virtual_query_method}

    def _query_method_get(self):
        methods = self._query_methods()
        return methods.get(self.code)

    def _invalid_methods(self):
        # to override.
        # update other  _query_methods
        return {'virtual': virtual_invalid_method}

    def _invalid_method_get(self):
        methods = self._invalid_methods()
        return methods.get(self.code)

    def _print_methods(self):
        # to override.
        # update other  _query_methods
        return {'virtual': virtual_print_method}

    def _print_method_get(self):
        methods = self._print_methods()
        return methods.get(self.code)


class ApiDrawCompany(models.Model):
    _name = "fp.api.draw.company"
    _description = "Api Draw Company"

    company_id = fields.Many2one(
        'res.company', 'Company', required=True, index=True,
        default=lambda self: self.env.company)

    name = fields.Char(
        string='Name', related='company_id.name', store=True)

    tin = fields.Char(
        string='TIN', related="company_id.partner_id.vat", store=True)

    # draw_provider_id
    api_draw_id = fields.Many2one(
        'fp.api.draw', string='Api Draw')

    @api.model
    def search_by_company(self, company_id):
        return self.search([('company_id', '=', company_id.id)], limit=1)

    _sql_constraints = [
        ('company_id_key', 'UNIQUE (company_id)',
         'You can not set two times for the same company !')
    ]
